import { Message } from 'element-ui';
import { Tools } from '@/utils/tools';
import router from '@/router/index';
// const common:string[] = ['channelId', 'keyWord', 'moduleId', 'priceTypeId']
// 扫描接口
// 公共 【发货单/库存调整】 [自动扫描/sku表头 全局]
export default {
    // 自动识别
    auto: '/bill/scan/automaticIdentification',
    // sku表头
    skuHeader: '/bill/scan/getGoodsSkuHeader',
    // 获取请求接口
    requestInterface: (
        type:
            | 'public'
            | 'noticeBill'
            | 'sendBill'
            | 'purchaseReceiveBill'
            | 'inventoryBill'
            | 'packingBill'
            | 'receiveBill'
            | 'purchaseReturnBill'
            | 'salePlanAdjustBill'
            | 'purchaseReturnNoticeBill',
        name: 'browse' | 'barCode' | 'box' | 'skuPage' | 'onlyKey' | 'sku'
    ) => {
        const type_ = type === 'public' ? 'scan' : type;
        const prefix = [
            'public',
            'noticeBill',
            'inventoryBill',
            'purchaseReturnBill',
            'purchaseReturnNoticeBill',
            'salePlanAdjustBill',
            'receiveBill',
            'purchaseReceiveBill',
            'sendBill'
        ].includes(type)
            ? 'bill'
            : 'information';
        const api = {
            // 录入货品
            browse: `${prefix}/${type_}/browseGoodsPage`,
            // 扫描条形码
            barCode: `/${prefix}/${type_}/scanBarcode`,
            // 扫描箱子
            box: `/${prefix}/${type_}/scanBox`,
            // sku分页列表
            skuPage: `/${prefix}/${type_}/scanGoodsPage`,
            // 扫描唯一码
            onlyKey: `/${prefix}/${type_}/scanLabelCode`,
            // 录入sku
            sku: `/${prefix}/${type_}/scanSku`
        };
        return api[name];
    },
    // 参数约束 在模块内【this.cancelCheckParameter = ['priceTypeId'] 可以动态解除某个约束】 这里后续会优化
    parameterConstraint: {
        public: {
            browse: ['channelId', 'keyWord', 'moduleId', 'priceTypeId'],
            barCode: ['channelId', 'keyWord', 'moduleId', 'priceTypeId', 'quantity', 'billDate'],
            box: ['keyWord', 'moduleId', 'priceTypeId', 'channelId', 'billDate'],
            skuPage: ['channelId', 'moduleId', 'priceTypeId', 'keyWord'],
            onlyKey: ['channelId', 'keyWord', 'moduleId', 'priceTypeId', 'quantity', 'billDate'],
            sku: ['channelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'priceTypeId', 'quantity', 'sizeId', 'billDate']
        },
        salePlanAdjustBill: {
            browse: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity'],
            skuPage: ['billIdList', 'keyWord'],
            sku: ['billIdList', 'channelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'quantity', 'sizeId', 'billDate'],
            barCode: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billDate']
        },
        noticeBill: {
            browse: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'priceTypeId', 'quantity'],
            skuPage: ['billIdList', 'keyWord'],
            sku: ['billIdList', 'channelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'priceTypeId', 'quantity', 'sizeId', 'billDate'],
            barCode: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'priceTypeId', 'quantity', 'billDate']
        },
        sendBill: {
            browse: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'priceTypeId'],
            barCode: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'priceTypeId', 'quantity', 'billDate'],
            box: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'priceTypeId', 'billDate'],
            skuPage: ['billIdList', 'toChannelId', 'moduleId', 'priceTypeId', 'keyWord'],
            onlyKey: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'priceTypeId', 'quantity', 'billDate'],
            sku: ['billIdList', 'toChannelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'priceTypeId', 'quantity', 'sizeId', 'billDate']
        },
        // 收货
        receiveBill: {
            browse: ['billIdList', 'toChannelId', 'keyWord', 'moduleId'],
            barCode: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            box: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'billDate'],
            skuPage: ['billIdList', 'toChannelId', 'moduleId', 'keyWord'],
            onlyKey: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            sku: ['billIdList', 'toChannelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'quantity', 'sizeId', 'billDate']
        },
        purchaseReceiveBill: {
            browse: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'type'],
            barCode: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'quantity', 'type', 'billDate'],
            box: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'billDate'],
            skuPage: ['billIdList', 'toChannelId', 'moduleId', 'keyWord', 'type'],
            onlyKey: ['billIdList', 'toChannelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            sku: ['billIdList', 'toChannelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'type', 'quantity', 'billDate', 'sizeId']
        },
        inventoryBill: {
            browse: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'billRedisKey'],
            barCode: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billRedisKey', 'billDate'],
            box: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'billRedisKey', 'billDate'],
            skuPage: ['billIdList', 'channelId', 'moduleId', 'keyWord', 'billRedisKey'],
            onlyKey: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billRedisKey', 'billDate'],
            sku: ['billIdList', 'channelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'quantity', 'sizeId', 'billDate', 'billRedisKey']
        },
        packingBill: {
            barCode: ['billIdList', 'boxId', 'channelId', 'billDate', 'keyWord', 'moduleId', 'quantity', 'packingBillId'],
            box: ['billIdList', 'boxId', 'channelId', 'keyWord', 'moduleId', 'packingBillId', 'quantity', 'billDate'],
            skuPage: ['billIdList', 'moduleId', 'keyWord'],
            onlyKey: ['billDate', 'billIdList', 'boxId', 'channelId', 'keyWord', 'moduleId', 'quantity', 'packingBillId'],
            sku: [
                'billIdList',
                'boxId',
                'channelId',
                'colorId',
                'moduleId',
                'goodsId',
                'longId',
                'quantity',
                'sizeId',
                'packingBillId',
                'quantity',
                'billDate',
                'fullSizeFlag'
            ]
        },
        // 采购退货通知单
        purchaseReturnNoticeBill: {
            browse: ['billIdList', 'channelId', 'keyWord', 'moduleId'],
            barCode: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            box: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'billDate'],
            skuPage: ['billIdList', 'channelId', 'moduleId', 'keyWord'],
            onlyKey: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            sku: ['billDate', 'billIdList', 'channelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'quantity', 'sizeId']
        },
        // 采购退单
        purchaseReturnBill: {
            browse: ['billIdList', 'channelId', 'keyWord', 'moduleId'],
            barCode: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            box: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'billDate'],
            skuPage: ['billIdList', 'channelId', 'moduleId', 'keyWord'],
            onlyKey: ['billIdList', 'channelId', 'keyWord', 'moduleId', 'quantity', 'billDate'],
            sku: ['billDate', 'billIdList', 'channelId', 'colorId', 'moduleId', 'goodsId', 'longId', 'quantity', 'sizeId']
        }
    },
    requestCheck: (array, object, type) => {
        const messageMap = (key, type) => {
            switch (true) {
                case key === 'channelId':
                    return '请选择渠道';
                case key === 'keyWord':
                    return '请输入关键词';
                case key === 'priceTypeId':
                    return '请选择价格类型';
                case key === 'toChannelId':
                    return '请选择到货渠道';
                case key === 'boxId':
                    return '请选择需要配货的箱号';
                case key === 'billDate':
                    return '请选择单据日期';
                case key === 'billIdList' && type === 'inventoryBill':
                    return '请选择盘点计划';
                case key === 'billIdList' && type === 'receiveBill':
                    return '请选择发货单';
                case key === 'billIdList' && type === 'noticeBill':
                    return '请选择销售计划单号';

                default:
                    return `缺少${key}参数`;
            }
        };
        const obj: any = {};
        const NotMustCheck = ['colorId', 'longId', 'sizeId', 'moduleId', 'keyWord', 'type'];
        for (let a = 0; a < array.length; a++) {
            if (Tools.isEmpty(object[array[a]])) {
                const currentRouterModuleId = Number(router['history'].current.meta.baseModuleId || router['history'].current.meta.moduleId);
                const PURCHASE_MODULE_IDS: number[] = [700013, 700014, 700015];
                switch (true) {
                    // 指令单 发货单【单据】可选+可不选=>有指令/无指令 装箱单 无指令/有指令 到货通知单 无指令/有指令 单号都是可选
                    case ['sendBill', 'noticeBill', 'packingBill', 'purchaseReceiveBill', 'purchaseReturnNoticeBill', 'purchaseReturnBill'].includes(
                        type
                    ) && array[a] === 'billIdList':
                        obj[array[a]] = object[array[a]] || undefined;
                        break;
                    // 采购单/采购调整 验证跳过渠道和价格类型
                    case PURCHASE_MODULE_IDS.includes(currentRouterModuleId) && ['priceTypeId', 'channelId'].includes(array[a]):
                        obj[array[a]] = object[array[a]] || undefined;
                        break;
                    // 非比检验项
                    case NotMustCheck.includes(array[a]):
                        obj[array[a]] = object[array[a]] || undefined;
                        break;
                    // 默认
                    default:
                        Message.error(messageMap(array[a], type));
                        return false;
                }
            } else if (array[a] === 'toChannelId') {
                obj['channelId'] = object[array[a]];
            } else {
                obj[array[a]] = object[array[a]];
            }
        }
        // 处理单据列表 转Long类型
        if (Object.keys(obj).includes('billIdList') && obj['billIdList']) {
            obj['billIdList'] = object['billIdList'].map((em) => {
                return parseFloat(em);
            });
        }
        // 处理模块id概率丢失问题/axios全局拦截也有
        // !obj.moduleId && (obj.moduleId = router['history'].current.meta.moduleId)
        return obj;
    }
};
